package org.springblade.task.quartz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.common.utils.DateUtils;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.mapper.QuartzTaskMapper;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: TODO
 * @author syncwt
 * @date ${date} ${time}
 * @version V1.0
 */
@Service
public class QuartzTaskServiceImpl extends ServiceImpl<QuartzTaskMapper, QuartzTask> implements IQuartzTaskService {

	@Autowired
	private QuartzTaskMapper quartzTaskMapper;

	/**
	 * 查询任务类
	 * @param taskName
	 * @param amount
	 * @return
	 */
	public QuartzTask queryByTaskName(String taskName, int amount) {
		QuartzTask quartzTask = quartzTaskMapper.queryByTaskName(taskName);
		if (quartzTask == null) {
			quartzTask = new QuartzTask();
			quartzTask.setTaskName(taskName);
			// 如果任务时间为空, 当前日期-7天
			quartzTask.setTaskDate(DateUtils.dateAddDay("", amount));
			quartzTask.setSubscript(0);
			quartzTask.setCreateTime(new Date());
		}
		return quartzTask;
	}

	public void saveQuartzTask(QuartzTask record) {

		record.setUpdateTime(new Date());
		quartzTaskMapper.saveQuartzTask(record);
	}
}
